﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using MedicinaMuncii.BL.Contracts;
using log4net;
using MedicinaMuncii.UI.Mvc.Util;
using System.Web.Routing;
using System.Web.Mvc;


namespace MedicinaMuncii.UI.Mvc
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        #region Fields
        /// <summary>
        /// Member for providing log functionality to this class
        /// </summary>
        protected readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion Fields

        #region Event Handlers
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);

            ConfigureLogger();
            ConfigureContainer();
        }

        /// <summary>
        /// Handler for errors that unexpectedly occur in the application
        /// </summary>
        protected void Application_Error()
        {
            Exception exception = HttpContext.Current.Server.GetLastError();

            if (exception != null)
            {
                log.Error(exception);
            }
        }
        #endregion Event Handlers

        #region Helper Methods
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Application", action = "AptitudeForm", id = UrlParameter.Optional } // Parameter defaults
            );
        }

        private void ConfigureContainer()
        {
            ControllerBuilder.Current.SetControllerFactory(typeof(CustomControllerFactory));
        }

        private void ConfigureLogger()
        {
            log4net.Config.XmlConfigurator.Configure();
            log.Info("Application started.");
        }
        #endregion Helper Methods
    }    
}